<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .demo {
      width: 200px;
      height: 200px;
      background-color: #6ff;
      margin: 150px;
      /* position: absolute;
            left: 100px;
            top: 100px; */
      /* display: none; */
    }
  </style>
</head>

<body>
  <div class="demo"></div>
</body>
<script>
  // 获取元素的偏移 (margin position)
  var demo = document.getElementsByClassName("demo")[0];

  // 传统的方法
  // var marginLeft = parseInt(getStyle(demo,"margin-left"));
  // var marginTop = parseInt(getStyle(demo,"margin-top"));
  // console.log(marginLeft,marginTop);

  // var posLeft = parseInt(getStyle(demo,"left"));
  // var posTop = parseInt(getStyle(demo,"top"));
  // console.log(posLeft,posTop);

  console.dir(demo);
  // offsetLeft offsetTop   获取元素的偏移相对于参照物父元素的偏移 (margin和position)
  // 优点: 快捷取值

  // 缺点
  // 1. 只能取值不能赋值
  // 2. 对于隐藏(display: none;)的元素 offsetLeft offsetTop   不生效 => 0

  console.log(demo.offsetParent); // body 
  console.log(demo.offsetLeft, demo.offsetTop);  // 相对于body的偏移














  function getStyle(ele, attr) {
    if (window.getComputedStyle) {
      return window.getComputedStyle(ele)[attr];
    } else {
      return ele.currentStyle[attr];
    }
  }


</script>

</html>